﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace ServerApp
{
    public partial class Form1 : Form
    {
        string Data = "";
       static UDPServices udpServices = new UDPServices();
       static NetworkStream clientStream;
       static bool write = false;
       public String abc;

       
        public Form1()
        {
            InitializeComponent();

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            //try
            //{
            //    udpServices.server_Load();

            //}
            //catch (Exception ex)
            //{
            //    // MessageBox.Show(ex.ToString());
            //}
         
        }
        public void returnIP()
        {
            String abc = udpServices.returnIP();
            MessageBox.Show("IP is : " + abc);
        }

        
        public void run()
        {
            TcpClient client = new TcpClient();
            IPEndPoint serverEndpoint = new IPEndPoint(IPAddress.Parse("192.168.1.146"), 8080);
            client.Connect(serverEndpoint);
            clientStream = client.GetStream();
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer;
            while (true)
            {
                buffer = encoder.GetBytes("GO");
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();
            }
        }

        private void btnBrower_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog b = new FolderBrowserDialog();
            b.ShowDialog() ;
             txtPath.Text = b.SelectedPath;
             TxtPath.Text = b.SelectedPath;
            
            string path = b.SelectedPath;
            DirectoryInfo dInfo = new DirectoryInfo(path);
            FileInfo[] files = dInfo.GetFiles();
            
            for (int i = 0; i < files.Length; i++)
            {
                string filename = files[i].Name;
                int length = filename.LastIndexOf(".");// lấy chiều dài của file
                // từ đầu đến dấu chấm
                String DuoiFile = filename.Substring(length + 1);
                if (DuoiFile.Equals("ppt") || DuoiFile.Equals("pptx"))
                {
                    string path_filename = path + filename;
                    Data += path_filename;
                    Data += ";";
                }
            }


 
            
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                udpServices.FolderPath = txtPath.Text;
                udpServices.server_Load();
                abc = txtPath.Text;

            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.ToString());
            }
        }
      
        
    }
}
